<!DOCTYPE html>
<?php require dirname(dirname(__FILE__)) . '/view/checkuser.php';?>
<html>
<head>
<meta charset="UTF-8">
<title>188对比</title>
<link rel="stylesheet" type="text/css" href="../include/css/base.css" />
<link href="/include/css/smoothness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet">
<script type="text/javascript" src="/include/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="/include/js/jquery-ui-1.10.3.custom.min.js"></script>
<script type="text/javascript" src="/include/js/jquery.ui.datepicker-zh-cn.js"></script>
<script type="text/javascript">
$(function(){
    $("#time_start").datepicker({
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });
    $("#time_end").datepicker({
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });
    
});
</script>
</head>
<body>
<?php
set_time_limit(90);
$nowtime = time();
$time_start = isset($_GET['time_start']) ? $_GET['time_start'] : date('Y-m-d', $nowtime);
$time_end = isset($_GET['time_end']) ? $_GET['time_end'] : date('Y-m-d', $nowtime);
$time_start_stamp = strtotime($time_start);
$time_end_stamp = strtotime($time_end);
if ($time_start_stamp == false || $time_end_stamp == false){
	exit('时间参数错误！');
}
if ((($time_end_stamp - $time_start_stamp) / 86400) > 4){
	exit('最多支持5天数据查询！');
}

require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/basefunc.php';
require dirname(dirname(__FILE__)) . '/include/database.php';

$db = db_touzhu();

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	foreach ($_POST as $_key => $one){
		@list($_postitem, $_home_team_id, $_visiting_team_id, $date_set) = explode('_', $_key);
		if ($_postitem == 'matchre' && $_home_team_id > 0 && $_visiting_team_id > 0 && is_numeric($one) && is_numeric($date_set) && $date_set > 0){
			$_date_start = date('Y-m-d 22:00:00', $date_set - 86400); // 包括前一天的俩小时
			$_date_end = date('Y-m-d 02:00:00', $date_set + 86400); // 包括后一天的俩小时
			$sql_up = "update ball_disc set match_result={$one} where home_team_id={$_home_team_id} and visiting_team_id={$_visiting_team_id} and fetch_time>='{$_date_start}' and fetch_time<='{$_date_end}'";
			$db->query($sql_up);
		}
	}
	exit('成功');
}
?>
<form action="" method="get">
日期：
<input type="text" id="time_start" name="time_start" value="<?php echo $time_start?>" size="8" /> - 
<input type="text" id="time_end" name="time_end" value="<?php echo $time_end?>" size="8" />
<input type="submit" value=" GO " />
</form>

<form action="" method="post">
<table class="tableunify">
	<thead>
		<tr>
			<th>联赛名称</th>
			<th>主队</th>
			<th>客队</th>
			<th>比赛开始时间</th>
			<th>盘口</th>
			<th>大水</th>
			<th>小水</th>
			<th>抓取时间1</th>
			<th>盘口</th>
			<th>大水</th>
			<th>小水</th>
			<th>抓取时间2</th>
			<th>盘口</th>
			<th>大水</th>
			<th>小水</th>
			<th>早减晚</th>
			<th>比赛结果</th>
		</tr>
	</thead>
	<tbody>
	<?php
	$time_start_sql = date('Y-m-d 12:00:00', $time_start_stamp);
	$time_end_sql = date('Y-m-d 11:59:59', $time_end_stamp + 86400);
	$sql = 'select id, match_time, match_id, match_name, home_team, visiting_team, home_team_id, visiting_team_id, pankou, over, under, fetch_time, match_result from ball_disc ';
	$sql .= "where `from`=2 and wanfa=4 and home_team_id>0 and visiting_team_id>0 and fetch_time>='{$time_start_sql}' and fetch_time<='{$time_end_sql}' order by home_team_id, id";
	$re = $db->query($sql);
	
	$compare_arr = array();
	if (!empty($re)){
		$first = $HTfirst = $HTlast = $first_fetch_time = array();
		foreach ($re as $per){
			//echo "{$per['match_time']}\t{$per['home_team_id']}\t{$per['visiting_team_id']}\t{$per['home_team']}\t{$per['visiting_team']}\t{$per['pankou']}\t{$per['over']}\t{$per['under']}\n";
			$key_match = "{$per['home_team_id']}_{$per['visiting_team_id']}_" . substr($per['fetch_time'], 0, 10);
			if (!isset($first[$key_match]) && $per['match_time'] != 'H.T' && $per['match_time'] != ''){
				if ($per['pankou'] > 0 && $per['over'] > 0 && $per['under'] > 0){
					$first[$key_match] =  $per;
					$first_fetch_time[] = $per['fetch_time'];
				}
			}
			if ($per['match_time'] == 'H.T'){
				if (!isset($HTfirst[$key_match])){
					$HTfirst[$key_match] = $per;
				}
				$HTlast[$key_match] = $per;
			}
		}
		array_multisort($first_fetch_time, $first);
		$counter = 0;
		foreach ($first as $key => $val){
			if (!isset($HTfirst[$key]) || !isset($HTlast[$key])){
				continue;
			}
			// 最早大水减最晚大水
			$compare_over = round($HTfirst[$key]['over'] - $HTlast[$key]['over'], 2);
			$is_alt = ($counter ++ % 2) ? ' class="alt"' : '';
			$match_date = strtotime($val['fetch_time']);
			echo "<tr {$is_alt}><td>{$val['match_name']}</td><td>{$val['home_team']}</td><td>{$val['visiting_team']}</td>
				<td>{$val['fetch_time']}</td><td>{$val['pankou']}</td><td>{$val['over']}</td><td>{$val['under']}</td>
				<td>{$HTfirst[$key]['fetch_time']}</td><td>{$HTfirst[$key]['pankou']}</td><td>{$HTfirst[$key]['over']}</td><td>{$HTfirst[$key]['under']}</td>
				<td>{$HTlast[$key]['fetch_time']}</td><td>{$HTlast[$key]['pankou']}</td><td>{$HTlast[$key]['over']}</td><td>{$HTlast[$key]['under']}</td>
				<td>{$compare_over}</td><td>" . 
				($val['match_result'] > -1 ? $val['match_result'] : '') . '<input type="text" name="matchre_' . $val['home_team_id'] . '_' . $val['visiting_team_id'] . '_' . $match_date . '" value="" size="1" />' .
				"</td></tr>\n";
		}
		
	}
	?>
	</tbody>
	<tfoot>
		<tr>
		   <th class="paging" colspan="17"><input type="submit" value=" 保 存 " /></th>
		</tr>
	</tfoot>
</table>
</form>

</body>
</html>